/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.converters;

import cz.muni.fi.pa165.machineryrental.dto.RentalDTO;
import cz.muni.fi.pa165.machineryrental.entities.Rental;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 
 */
public class RentalDTOConverter {
    
    /**
     * Converts Rental entity to RentalDTO
     */
    public static RentalDTO toDTO(Rental entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        RentalDTO dto = new RentalDTO();
        return applyToDTO(dto, entity);
	}

    /**
     * Converts RentalDTO to Rental entity
     */
    public static Rental toEntity(RentalDTO dto) {
        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        Rental entity = new Rental();
        entity.setId(dto.getId());
        entity.setDateOfRent(dto.getDateOfRent());
        entity.setDateOfReturn(dto.getDateOfReturn());
        entity.setDuration(dto.getDuration());
		
		if (dto.getCustomer() != null) {
			entity.setCustomer(CustomerDTOConverter.toEntity(dto.getCustomer()));
		}
		
		if (dto.getMachine() != null) {
			entity.setMachine(MachineDTOConverter.toEntity(dto.getMachine()));
		}
		
        return entity;
    }

    /**
     * Overwrites DTO with information in entity
     */
    public static RentalDTO applyToDTO(RentalDTO dto, Rental entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        dto.setId(entity.getId());
        dto.setDateOfRent(entity.getDateOfRent());
        dto.setDateOfReturn(entity.getDateOfReturn());
        dto.setDuration(entity.getDuration());

		if (entity.getCustomer() != null && dto.getCustomer() == null) {
			dto.setCustomer(CustomerDTOConverter.toDTO(entity.getCustomer()));
		}
		
		if (entity.getMachine() != null && dto.getMachine() == null) {
			dto.setMachine(MachineDTOConverter.toDTO(entity.getMachine()));
		}

        return dto;
    }
    
    /**
     * Converts list of rental entities to list of rental DTOs.
     *
     * @param rentals Rentals to convert.
     * @return List of DTOs according to given rentals.
     */
    public static List<RentalDTO> toDTOList(List<Rental> rentals) {
        if (rentals == null || rentals.isEmpty()) {
            return new ArrayList<RentalDTO>();
        }

        List<RentalDTO> result = new ArrayList<RentalDTO>();
        for (Rental r : rentals) {
            result.add(RentalDTOConverter.toDTO(r));
        }

        return result;
    }
	
	/**
	 * Converts list of rental dtos to list of rental entities
	 * 
	 * @param rentals List of rental DTOs
	 * @return List of rental entities
	 */
	public static List<Rental> toEntityList(List<RentalDTO> rentals) {
		if (rentals == null || rentals.isEmpty()) {
			return new ArrayList<Rental>();
		}
		
		List<Rental> result = new ArrayList<Rental>();
		for (RentalDTO r : rentals) {
			result.add(RentalDTOConverter.toEntity(r));
		}
		
		return result;
	}
}
